package com.dmm.vod.service.impl;

import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmm.result.Result;
import com.dmm.vo.vod.SubjectEeVo;
import com.dmm.vod.entity.Subject;
import com.dmm.vod.mapper.SubjectMapper;
import com.dmm.vod.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmm.vod.utils.SubjectListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author dmm
 * @since 2022-08-26
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    SubjectMapper subjectMapper;

    @Autowired
    SubjectListener subjectListener;
    @Override
    public List<Subject> selectList(Long id) {

        QueryWrapper<Subject> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Subject> list = subjectMapper.selectList(queryWrapper);

        List<Long> groupsummation = subjectMapper.Groupsummation();

        System.out.println(groupsummation.get(0));

        for (Subject subjects: list){

            boolean b=groupsummation.contains(subjects.getId());
            if(b){
                subjects.setHasChildren(true);
            }
        }

        return list;
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException{


        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> dictList = baseMapper.selectList(null);

            System.out.println(dictList);
            EasyExcel.write(response.getOutputStream(), Subject.class).sheet("模板").doWrite(dictList);

        } catch (Exception e) {

        }
    }

    @Override
    public void importDictData(MultipartFile file) {
        try {

            EasyExcel.read(file.getInputStream(),
                    Subject.class,subjectListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
